/**
 * 日期范围工具类
 */
const Datetimeformat = (date) => {
    date = new Date(date)
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    let d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    let h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    let minutes = date.getMinutes();
    minutes = minutes < 10 ? ('0' + minutes) : minutes;
    let s = date.getSeconds()
    s = s < 10 ? ('0' + s) : s;

    return `${y}-${m}-${d} ${h}:${minutes}:${s}`
}

export const getBeginToday = () => {
  return new Date(new Date(new Date().toLocaleDateString()).getTime())
}

export const startTime = (time) => {
    const nowTimeDate = new Date(time)
    return Datetimeformat(nowTimeDate.setHours(0, 0, 0, 0))
}

export const endTime = (time) => {
    const nowTimeDate = new Date(time)
    return Datetimeformat(nowTimeDate.setHours(23, 59, 59, 999))
}

export default {
  /***
   * 当前时间
   */
   getCurrentDate() {
    return new Date();
  },

  /***
   * 今天的开始时间
   */
   getstartToday() {
    return startTime(getBeginToday());
  },

  /***
   * 今天的结束时间
   */
  getEndToday() {
    return endTime(getBeginToday());
  },

  /***
   * 昨天开始时间
   */
   getBeginYesterday() {
    return startTime(getBeginToday() - 24 * 60 * 60 * 1000);
  },


  /***
   * 昨天结束时间时间
   */
   getEndYesterday() {
    return endTime(getBeginToday() - 24 * 60 * 60 * 1000);
  },
  
  /**
   * 获取过去7天
   */
  getBeginSevenday() {
	  return startTime(getBeginToday() - 144 * 60 * 60 * 1000);
  },
  getEndSevenday() {
  	  return endTime(getBeginToday());
  },
  
  /**
   * 获取过去30天
   */
  
  getBeginThirtyday() {
  	  return startTime(getBeginToday() - 696 * 60 * 60 * 1000);
  },
  getEndThirtyday() {
  	  return endTime(getBeginToday());
  },
  
  
  /***
   * 本周的第一天时间
   */
   getBeginWeek() {
    let currentDate = this.getCurrentDate();
    let week = currentDate.getDay();

    //一天的毫秒数
    let millisecond = 1000 * 60 * 60 * 24;
    //减去的天数
    let minusDay = week != 0 ? week - 1 : 6;
    //本周 周一
    let monday = new Date(currentDate.getTime() - (minusDay * millisecond));
    return startTime(monday);
  },

  /***
   * 本周的最后一天时间
   */
   getEndWeek() {
    let currentDate = this.getCurrentDate();
    let week = currentDate.getDay();
    //一天的毫秒数
    let millisecond = 1000 * 60 * 60 * 24;
    //减去的天数
    let minusDay = week != 0 ? week - 1 : 6;
    //本周 周日
    let monday = new Date(currentDate.getTime() - (minusDay * millisecond));
    let sunday = new Date(monday.getTime() + (6 * millisecond));
    //返回
    return endTime(sunday);
  },

  /***
   * 上周的开始
   */
   getBeginLastWeek() {
    let currentDate = this.getCurrentDate();
    let first = currentDate.getDate() - currentDate.getDay() - 6;
    let startDate = new Date(currentDate.setDate(first));
    return startTime(startDate);
  },

  /***
   * 上周的结束
   */
   getEndLastWeek() {
    let currentDate = this.getCurrentDate();
    let first = currentDate.getDate() - currentDate.getDay() - 6;
    let last = first + 6;
    let endDate = new Date(currentDate.setDate(last));
    return endTime(endDate);
  },

  /***
   * 本月的第一天时间
   */
   getBeginMonth() {
    let currentDate = this.getCurrentDate();
    let currentMonth = currentDate.getMonth();
    //获得当前年份4位年
    let currentYear = currentDate.getFullYear();
    //求出本月第一天
    let firstDay = new Date(currentYear, currentMonth, 1);

    return startTime(firstDay);
  },

  /***
   * 本月的最后一天时间
   */
   getEndMonth() {
    //获取当前时间
    let currentDate = this.getCurrentDate();
    let fullYear = currentDate.getFullYear();
    let month = currentDate.getMonth() + 1; // getMonth 方法返回 0-11，代表1-12月
    let endOfMonth = new Date(fullYear, month, 0);
    return endTime(endOfMonth);
  },

  /***
   * 上月的第一天时间
   */
   getBeginLastMonth() {
    //获取当前时间
    let currentDate = this.getCurrentDate();
    //获得当前月份0-11
    let currentMonth = currentDate.getMonth();
    //获得当前年份4位年
    let currentYear = currentDate.getFullYear();
    //获得上一个月的第一天
    let priorMonthFirstDay = getPriorMonthFirstDay(currentYear, currentMonth);
    return priorMonthFirstDay;
  },

  /***
   * 上月的最后一天时间
   */
   getEndLastMonth() {
    //获取当前时间
    let currentDate = this.getCurrentDate();
    //获得当前月份0-11
    let currentMonth = currentDate.getMonth();
    //获得当前年份4位年
    let currentYear = currentDate.getFullYear();

    //当为12月的时候年份需要加1
    //月份需要更新为0 也就是下一年的第一个月
    if (currentMonth == 11) {
      currentYear++;
      currentMonth = 0; //就为
    } else {
      //否则只是月份增加,以便求的下一月的第一天
      currentMonth++;
    }

    //一天的毫秒数
    let millisecond = 1000 * 60 * 60 * 24;
    //求出上月的最后一天
    let lastDay = new Date(this.getBeginMonth().getTime() - millisecond);

    return endTime(lastDay);
  },

  /**
   * 返回上一个月的第一天Date类型
   * @param year 年
   * @param month 月
   **/
   getPriorMonthFirstDay(year, month) {
    //年份为0代表,是本年的第一月,所以不能减
    if (month == 0) {
      month = 11; //月份为上年的最后月份
      year--; //年份减1
      return new Date(year, month, 1);
    }
    //否则,只减去月份
    month--;
    return new Date(year, month, 1);
  }
}
